package com.gxa.admin.recycling.controller;

import com.gxa.admin.recycling.service.OrderService;
import com.gxa.admin.recycling.service.WasteService;
import com.gxa.admin.recycling.service.WasteTypeService;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.pojo.WasteType;
import com.gxa.core.recycling.util.StringUtil;
import com.gxa.core.recycling.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Map;

/**
 * @author R
 * @data 2021-12-07 16:46
 */
@RestController
@RequestMapping("/orderCount")
@Api(tags = "出入库统计")
public class OrderController {

    @Autowired
    OrderService service;

    @Autowired
    WasteService wasteService;

    @Autowired
    WasteTypeService wasteTypeService;

    @PostMapping("/getCount")
    @ApiOperation("查询出入库统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "统计方式",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "wasteType",value = "物资类型id",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "wasteName",value = "废品名字id",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "villageName",value = "小区名字",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "cycle",value = "统计周期",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "from",value = "自定义起止时间",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "to",value = "自定义结束时间",required = false,dataType = "String",paramType = "query")
    })
    public Map getCount(String type,Integer wasteType,Integer wasteName,String villageName,String cycle,String from,String to){
        System.out.println("wasteType:"+wasteType);
        System.out.println("wasteName:"+wasteName);


        String wasteNameById = null;
        String wasteTypeById = null;
        if (wasteType != null){
            WasteType wasteType1 = wasteTypeService.selectById(wasteType);
            wasteTypeById = wasteType1.getTypeName();
        }
        if (wasteName != null){
            wasteNameById = wasteService.selectById(wasteName).getName();
        }
        String end = null;
        String start = null;
        Map<String, Object> count = null;
        System.out.println("cycle:"+cycle);
        if (!StringUtil.isEmpty(cycle)){
            end = new Timestamp(System.currentTimeMillis()).toString();
            start = TimeUtil.rollBackTime(cycle);
        }
        if (type != null && type.equals("入库量统计")){
            count = service.getInOrder(wasteTypeById, wasteNameById, villageName, start, end, from, to);
        }else {
            count = service.getOutOrder(wasteTypeById, wasteNameById, villageName, start, end, from, to);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("200");
        jsonResult.setData(count);
        System.out.println(count);
        return jsonResult.getDataMap();
    }

}
